/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SPARQL;

import java.io.IOException;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 *
 * @author riccardo
 */
public class TriplePattern {
    
    private Subj s;
    private Pred p;
    private Obje o;
    
    public TriplePattern(Subj s, Pred p, Obje o) {
        this.s = s;
        this.p = p;
        this.o = o;
    }
    
    public Subj getS() {
        return s;
    }

    public Pred getP() {
        return p;
    }

    public Obje getO() {
        return o;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + Objects.hashCode(this.s);
        hash = 97 * hash + Objects.hashCode(this.p);
        hash = 97 * hash + Objects.hashCode(this.o);
        //System.out.println("hash code " + this + " " + hash);
        //try {
        //throw new IOException();
    //}
    //catch (IOException e) {
        //e.printStackTrace();
    //}
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        
        if (obj == null) {
            System.out.println("object null");
            return false;
        }
        if (getClass() != obj.getClass()) {
           System.out.println("false different classes " + this + " " + obj);
            return false;
        }
        final TriplePattern other = (TriplePattern) obj;
        if (!Objects.equals(this.s, other.s)) {
            System.out.println("false subject " + this.s + " " + other.s);
            return false;
        }
        if (!Objects.equals(this.p, other.p)) {
            System.out.println("false predicate " + this.p + " " + other.p);
            return false;
        }
        if (!Objects.equals(this.o, other.o)) {
            System.out.println("false object " + this.o + " " + other.o);
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return s + " " + p + " " + o;
    }
    
    @Override
    public Object clone(){
        return new TriplePattern((Subj) s.clone(), (Pred) p.clone(), (Obje) o.clone());
    }
    
    protected String toStringExecutable(){
        return s + " " + p + " " + o;
    }

    public void setS(Subj s) {
        this.s = s;
    }

    public void setP(Pred p) {
        this.p = p;
    }

    public void setO(Obje o) {
        this.o = o;
    }

  public Set<Constant> getConstants() {
        Set<Constant> constants = new HashSet<Constant>();
        if (this.getS() instanceof Constant)
            constants.add((Constant)this.getS());
        if (this.getP() instanceof Constant)
            constants.add((Constant)this.getP());
        if (this.getO() instanceof Constant)
            constants.add((Constant)this.getO());
        return constants;
    }
    
}
